/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jp.co.irep.juten.controller;

import java.util.Date;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.inject.Named;
import jp.co.irep.juten.utils.BeanUtil;
import jp.co.irep.juten.view.TemplateViewBean;
import jp.co.irep.juten.entity.Template;
import jp.co.irep.juten.facade.TemplateFacade;
import jp.co.irep.juten.utils.Consts;

/**
 *
 * @author giang_le
 */
@Named
public class TemplateControllerBean {

    @EJB
    TemplateFacade templateFacade;

    @Inject
    TemplateViewBean templateViewBean;

    public void find() {
        try {
            templateViewBean.setTemplateList(templateFacade.findAll());
            if (templateViewBean.getTemplateList().isEmpty()) {
                BeanUtil.setMessage(Consts.WARNING, BeanUtil.getMessageString("templateTableHasNoRecord"));
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            BeanUtil.setMessage(Consts.ERROR, BeanUtil.getMessageString("templateGetListError"));
        }
    }

    public String gotoEditPage() {
        setFormData(templateViewBean.getSelectedTemplate());
        return Consts.EDIT;
    }

    public String edit() {
        try {
            templateFacade.edit(getFormData());
            BeanUtil.setMessage(Consts.INFO, BeanUtil.getMessageString("templateUpdateSuccess"));
            return Consts.INDEX;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            BeanUtil.setMessage(Consts.ERROR, BeanUtil.getMessageString("templateUpdateError"));
            return null;
        }

    }

    public void delete() {
        try {
            templateFacade.remove(templateViewBean.getSelectedTemplate());
            BeanUtil.setMessage(Consts.INFO, BeanUtil.getMessageString("templateDeleteSuccess"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            BeanUtil.setMessage(Consts.ERROR, BeanUtil.getMessageString("templateDeleteError"));
        }

    }

    private void setFormData(Template template) {
        templateViewBean.setTemplateId(template.getId());
        templateViewBean.setTemplateName(template.getName());
        templateViewBean.setTemplateHtml(template.getHtml());
        templateViewBean.setTemplateDelFlg((template.getDeleteFlg() == 1));
        templateViewBean.setTemplateMemo(template.getMemo());
    }

    private Template getFormData() {
        Template template = new Template();

        template.setName(templateViewBean.getTemplateName().trim());
        template.setHtml(templateViewBean.getTemplateHtml());
        template.setDeleteFlg(templateViewBean.getTemplateDelFlg() == true ? 1 : 0);
        template.setMemo(templateViewBean.getTemplateMemo());
        template.setId(templateViewBean.getTemplateId());
        template.setUpdate(new Date());
        template.setDate(templateViewBean.getSelectedTemplate().getDate());

        return template;
    }

}
